package com.centaline.androidsalesblog.activities.mine;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.TabFragmentAdapter;
import com.centaline.androidsalesblog.base.BaseActivity;
import com.centaline.androidsalesblog.constants.AppConstant;
import com.centaline.androidsalesblog.constants.CommonConstant;
import com.centaline.androidsalesblog.constants.SprfConstant;
import com.centaline.androidsalesblog.fragments.collection.ActCollFragment;
import com.centaline.androidsalesblog.fragments.collection.EsfEstateCollFragment;
import com.centaline.androidsalesblog.fragments.collection.NewEstCollFragment;
import com.centaline.androidsalesblog.fragments.collection.RentCollFragment;
import com.centaline.androidsalesblog.fragments.collection.SaleCollFragment;
import com.centaline.androidsalesblog.model.databaseobject.BizUnitDo;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.centaline.androidsalesblog.widgets.IndexViewPager;
import com.centanet.framework.utils.SprfUtil;
import com.trello.rxlifecycle.ActivityEvent;

import java.util.ArrayList;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 收藏
 * Created by Ruan on 2016/3/18.
 */
public class CollectionActivity extends BaseActivity {

    private TabLayout tl_collection;
    private IndexViewPager indexViewPager;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_collection;
    }

    @Override
    protected void findViews() {
        tl_collection = (TabLayout) findViewById(R.id.tl_collection);
        indexViewPager = (IndexViewPager) findViewById(R.id.indexViewPager);
    }

    @Override
    protected void initViews() {
        setUniversalToolbar(R.string.collect);
        check();
    }

    private void check() {
        if (AppConstant.IsLogin) {
            initPager();
        } else {
            startActivityForResult(new Intent(this, LoginActivity.class), CommonConstant.REQ_LOGIN);
        }
    }

    private void initPager() {
        RxDataBase.getBizUnit()
                .compose(this.<BizUnitDo>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BizUnitDo>() {
                    @Override
                    public void call(BizUnitDo bizUnitDo) {
                        boolean enableErshoufang = bizUnitDo.isEnableErshoufang();
                        boolean enableNewprop = bizUnitDo.isEnableNewprop();
                        ArrayList<String> titleList = new ArrayList<>();
                        ArrayList<Fragment> fragments = new ArrayList<>();
                        boolean currentIndexEnable = false;
                        int currentIndex = 0;
                        int collectPosition = SprfUtil.getInt(CollectionActivity.this,
                                String.format("%s_%s", SprfConstant.COLLECT_POSITION, AppConstant.CityCode), 0);
                        if (enableErshoufang) {
                            titleList.add("二手房");
                            titleList.add("租房");
                            SaleCollFragment saleCollFragment = new SaleCollFragment();
                            RentCollFragment rentCollFragment = new RentCollFragment();
                            fragments.add(saleCollFragment);
                            fragments.add(rentCollFragment);
                            if (collectPosition == 11) {
                                currentIndexEnable = true;
                                currentIndex = 0;
                            }
                            if (collectPosition == 12) {
                                currentIndexEnable = true;
                                currentIndex = 1;
                            }
                        }
                        if (enableNewprop) {
                            titleList.add("新房");
                            NewEstCollFragment newEstCollFragment = new NewEstCollFragment();
                            fragments.add(newEstCollFragment);
                            if (collectPosition == 14) {
                                currentIndexEnable = true;
                                currentIndex = fragments.size() == 1 ? 0 : 2;
                            }
                        }
                        if (enableErshoufang) {
                            titleList.add("小区");
                            EsfEstateCollFragment esfEstateCollFragment = new EsfEstateCollFragment();
                            fragments.add(esfEstateCollFragment);
                            if (collectPosition == 13) {
                                currentIndexEnable = true;
                                currentIndex = fragments.size() == 4 ? 3 : 2;
                            }
                        }
                        if (enableNewprop) {
                            titleList.add("活动");
                            ActCollFragment actCollFragment = new ActCollFragment();
                            fragments.add(actCollFragment);
                            if (collectPosition == 15) {
                                currentIndexEnable = true;
                                currentIndex = fragments.size() == 5 ? 4 : 1;
                            }
                        }
                        indexViewPager.setAdapter(new TabFragmentAdapter(getSupportFragmentManager(), fragments, titleList));
                        tl_collection.setupWithViewPager(indexViewPager);

                        if (currentIndexEnable) {
                            indexViewPager.setCurrentItem(currentIndex);
                        }
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            switch (requestCode) {
                case CommonConstant.REQ_LOGIN:
                    check();
                    break;
                default:
                    break;
            }
        } else {
            finish();
        }
    }
}
